(function (vc) {
    vc.extends({
        propTypes: {
            callBackListener: vc.propTypes.string, //父组件名称
            callBackFunction: vc.propTypes.string //父组件监听方法
        },
        data: {
            addLabelInfo: {
                labelId: '',
                labelName: '',
                domains: [],
                domain: '',
                labelType: 'C'
            }
        },
        _initMethod: function () {
            vc.getDict('s_label', "domain", function (_data) {
                $that.addLabelInfo.domains = _data;
            });
        },
        _initEvent: function () {
            vc.on('addLabel', 'openAddLabelModal', function () {
                $('#addLabelModel').modal('show');
            });
        },
        methods: {
            addLabelValidate() {
                return vc.validate.validate({
                    addLabelInfo: $that.addLabelInfo
                }, {
                    'addLabelInfo.labelName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "标签名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "标签名称不能超过30位"
                        },
                    ],
                    'addLabelInfo.domain': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "域不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "域格式错误"
                        },
                    ],
                    'addLabelInfo.labelType': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "标签类型不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "12",
                            errInfo: "标签类型格式错误"
                        },
                    ],
                });
            },
            saveLabelInfo: function () {
                if (!$that.addLabelValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                $that.addLabelInfo.shopId = vc.getCurrentCommunity().shopId;
                //不提交数据将数据 回调给侦听处理
                if (vc.notNull($props.callBackListener)) {
                    vc.emit($props.callBackListener, $props.callBackFunction, $that.addLabelInfo);
                    $('#addLabelModel').modal('hide');
                    return;
                }
                vc.http.apiPost(
                    '/label/saveLabel',
                    JSON.stringify($that.addLabelInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addLabelModel').modal('hide');
                            $that.clearAddLabelInfo();
                            vc.emit('labelManage', 'listLabel', {});
                            vc.toast("添加成功");
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            clearAddLabelInfo: function () {
                $that.addLabelInfo = {
                    labelName: '',
                    domains: [],
                    domain: '',
                    labelType: 'C'
                };
            }
        }
    });
})(window.vc);
